<html>
<head>
    <meta charset="utf-8"/>
<meta name="description" content=""/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>

<title>Druid数据库连接池技术 | 絷缘的博客</title>

<link rel="shortcut icon" href="https://zydnc.gitee.io/favicon.ico?v=1732378515642">

<link href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://zydnc.gitee.io/styles/main.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css">

<script src="https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets/highlight.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.15.10/languages/dockerfile.min.js"></script>
<script src="https://cdn.bootcss.com/highlight.js/9.15.10/languages/dart.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/moment@2.27.0/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"></script>
<!-- DEMO JS -->
<!--<script src="media/scripts/index.js"></script>-->



    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.css">
 <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
</head>
<body>
<div class="main gt-bg-theme-color-first">
    <nav class="navbar navbar-expand-lg">
    <div class="navbar-brand">
        <img class="user-avatar" src="/images/avatar.png" alt="头像">
        <div class="site-name gt-c-content-color-first">
            絷缘的博客
        </div>
    </div>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <i class="fas fa-bars gt-c-content-color-first" style="font-size: 18px"></i>
    </button>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <div class="navbar-nav mr-auto" style="text-align: center">
            
                <div class="nav-item">
                    
                        <a href="/" class="menu gt-a-link">
                            首页
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/archives" class="menu gt-a-link">
                            归档
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/tags" class="menu gt-a-link">
                            标签
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/about" class="menu gt-a-link">
                            关于
                        </a>
                    
                </div>
            
                <div class="nav-item">
                    
                        <a href="/friends" class="menu gt-a-link">
                            友链
                        </a>
                    
                </div>
            
        </div>
        <div style="text-align: center">
            <form id="gridea-search-form" style="position: relative" data-update="1732378515642" action="/search/index.html">
                <input class="search-input" autocomplete="off" spellcheck="false" name="q" placeholder="搜索文章" />
                <i class="fas fa-search gt-c-content-color-first" style="position: absolute; top: 9px; left: 10px;"></i>
            </form>
        </div>
    </div>
</nav>

    <div class="post-container">
        <div class="post-detail gt-bg-theme-color-second">
            <article class="gt-post-content">
                <h2 class="post-title">
                    Druid数据库连接池技术
                </h2>
                <div class="post-info">
                    <time class="post-time gt-c-content-color-first">
                        · 2021-01-02 ·
                    </time>
                    
                        <a href="https://zydnc.gitee.io/D0jYG3ohU/" class="post-tags">
                            # Druid
                        </a>
                    
                        <a href="https://zydnc.gitee.io/java/" class="post-tags">
                            # Java
                        </a>
                    
                        <a href="https://zydnc.gitee.io/mysql/" class="post-tags">
                            # MySQL
                        </a>
                    
                </div>
                <div class="post-content">
                    <blockquote>
<p>本教程仅限于 <code>druidv1.2.4</code> + <code>mysql8.0</code> + <code>jdk1.8</code></p>
</blockquote>
<h2 id="一-在pomxml中加入druid依赖文件">一、在pom.xml中加入druid依赖文件</h2>
<pre><code class="language-xml">&lt;!-- https://mvnrepository.com/artifact/com.alibaba/druid --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;com.alibaba&lt;/groupId&gt;
    &lt;artifactId&gt;druid&lt;/artifactId&gt;
    &lt;version&gt;1.2.4&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<h2 id="二-书写配置文件">二、书写配置文件</h2>
<h3 id="1-druid数据库连接池技术对配置文件的命名没有要求因为它需要我们自己拿到配置文件的io流然后将io流作为参数传递创建数据库连接池对象">1. druid数据库连接池技术对配置文件的命名没有要求，因为它需要我们自己拿到配置文件的IO流，然后将IO流作为参数传递，创建数据库连接池对象</h3>
<h3 id="2-我就以-druidproperties-配置文件为例继续讲解">2. 我就以 druid.properties 配置文件为例，继续讲解</h3>
<pre><code class="language-properties">driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT
username=root
password=123456
#初始化连接数
initialSize=5
#最大连接数
maxActive=10
#超时时间
maxWait=3000
</code></pre>
<h2 id="三-书写代码测试正常的数据库连接对象的获取">三、书写代码，测试正常的数据库连接对象的获取</h2>
<p><code>为了方便看代码，我不去捕获异常，直接将异常抛出</code></p>
<pre><code class="language-java">public class DruidDemo{
    public static void main(String[] args) throws Exception{
        Properties pros = new Properties();
        pros.load(new BufferedReader(new InputStreamReader(DruidDemo.class.getClassLoader().getResourceAsStream(&quot;druid.properties&quot;),&quot;UTF-8&quot;)));
        DataSource ds = DruidDataSourceFactory.createDataSource(pros);
        Connection conn = ds.getConnection();
        System.out.println(conn);
    }
}
</code></pre>
<h2 id="四-进行一些crud操作">四、进行一些CRUD操作</h2>
<pre><code class="language-java">public class DruidDemo{
    public static void main(String[] args){
        Properties pros = new Properties();
        pros.load(new BufferedReader(new InputStreamReader(DruidDemo.class.getClassLoader().getResourceAsStream(&quot;druid.properties&quot;),&quot;UTF-8&quot;)));
        DataSource ds = DruidDataSourceFactory.createDataSource(pros);
        Connection conn = ds.getConnection();
        String sql = &quot;select * from user&quot;;
        PreparedStatement pstat = conn.prepareStatement(sql);
        ResultSet rs = pstat.executeQuery();
        while(rs.next()){
            System.out.println(rs.getInt(&quot;id&quot;)+&quot;\t&quot;+rs.getString(&quot;name&quot;)+&quot;\t&quot;+rs.getInt(&quot;age&quot;)+&quot;\t&quot;+rs.getString(&quot;gender&quot;)+&quot;\t&quot;);
        }
    }
}
</code></pre>
<blockquote>
<p>原文作者：絷缘<br>
作者邮箱：zhiyuanworkemail@163.com<br>
原文地址：<a href="https://zhiyuandnc.github.io/-gGSoZP8v/">https://zhiyuandnc.github.io/-gGSoZP8v/</a><br>
版权声明：本文为博主原创文章，转载请注明原文链接作者信息</p>
</blockquote>

                </div>
            </article>
        </div>

        
            <div class="next-post">
                <div class="next gt-c-content-color-first">下一篇</div>
                <a href="https://zydnc.gitee.io/c3p0/" class="post-title gt-a-link">
                    C3P0数据库连接池技术
                </a>
            </div>
        

        

        

        

        <div class="site-footer gt-c-content-color-first">
    <div class="slogan gt-c-content-color-first">事实不以人的意志为转移</div>
    <div class="social-container">
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-github gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-telegram gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-qq gt-c-content-color-first"></i>
                </a>
            
        
            
                <a href="#" target="_blank">
                    <i class="fab fa-bilibili gt-c-content-color-first"></i>
                </a>
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
    </div>
    <div class="footer-info">
        Copyright&ensp;&copy;&ensp;絷缘的博客<br/><a href="https://beian.miit.gov.cn/" target="_blank">晋ICP备20001723号-1</a>
    </div>
    <div>
        Theme by <a href="https://imhanjie.com/" target="_blank">imhanjie</a>, Powered by <a
                href="https://github.com/getgridea/gridea" target="_blank">Gridea | <a href="https://zydnc.gitee.io/atom.xml" target="_blank">RSS</a></a>
    </div>
</div>

<script>
  hljs.initHighlightingOnLoad()
</script>

    </div>
</div>
</body>
</html>
